﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using smartphone.App_Code;
using System.Text;

namespace smartphone.UserControls
{
    public partial class AdminEditProduct : System.Web.UI.UserControl
    {
        private Product product = new Product();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateControl();
            }
        }

        public void PopulateControl()
        {
            //Lay chi tiet san pham dang xem
            int productId = Int32.Parse(Request.QueryString["productID"].ToString());
            product = product.GetProduct(productId);

            //Lay danh sach hang san xuat
            Manufacturer manu = new Manufacturer();
            DropDownListManu.DataSource = manu.getManufacturer();
            DropDownListManu.DataTextField = "tenHang";
            DropDownListManu.DataValueField = "maHang";
            DropDownListManu.DataBind();
            foreach (ListItem item in DropDownListManu.Items)
            {
                if (item.Text == product.ManufacturerName)
                {
                    item.Selected = true;
                    break;
                }
            }

            //Lay danh sach khuyen mai
            Promotion promotion = new Promotion();
            DropDownListPromotion.DataSource = promotion.GetAllPromotion();
            DropDownListPromotion.DataTextField = "noiDung";
            DropDownListPromotion.DataValueField = "maKhuyenMai";
            DropDownListPromotion.DataBind();
            //phan tu dau tien cua danh sach khuyen mai la trong tuong duong voi san pham 
            //khong khuyen mai
            ListItem newitem = new ListItem();
            DropDownListPromotion.Items.Insert(0, newitem);

            //neu san co khuyen mai
            if (product.Promotion != "")
            {
                foreach (ListItem item in DropDownListManu.Items)
                {
                    if (item.Text == product.ManufacturerName)
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }

            
            

            TxtTitle.Text = product.ProductName;
            //FileUploadImage. = product.ProductImage;
            EditorDescription.Content = product.ProductDesciption;
            TxtQuantity.Text = product.ProductQuantity.ToString();
            TxtPrice.Text = product.ProductPrice.ToString("#,###");
            TxtImportDate.Text = product.ProductImportDate.ToShortDateString();
            TxtPK.Text = product.ProductAccessory;
            TxtWaranty.Text = product.ProductWarranty.ToString();
            CkRecord.Checked = product.ProductHasRecord;
            TxtOS.Text = product.ProductOS;
            TxtCpu.Text = product.ProductCPU;
            TxtCamera.Text = product.ProductCamera.ToString();
            TxtWifi.Text = product.ProductWifi;
            TxtMusic.Text = product.ProductAudio;
            TxtSensor.Text = product.ProductSensor;
            TxtMovie.Text = product.ProductMovie;
            CheckBoxPublish.Checked = product.Publish;
            //TxtDeal.Text = product.isDiscount.ToString();
        }

        protected void BtEditProductCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("AdminProductPage.aspx");
        }
    }
}